package edu.ccut.saturn.component.impl.sql;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import edu.ccut.saturn.component.ISaturnDataSqlItemInfo;
import edu.ccut.saturn.component.xml.ISaturnElement;
import edu.ccut.saturn.component.xml.SaturnElement;

public abstract class AbstractSqlItemInfo implements ISaturnDataSqlItemInfo {

	private String prepend;

	private List<ISaturnDataSqlItemInfo> subSqlItemInfos;

	public AbstractSqlItemInfo(String prepend) {
		this.prepend = prepend;
		this.subSqlItemInfos = new ArrayList<ISaturnDataSqlItemInfo>();
	}

	public String getPrepend() {
		return this.prepend;
	}

	public void setPrepend(String prepend) {
		this.prepend = prepend;
	}

	public abstract String getSubSql(Map<String, Object> param)
			throws Exception;

	public List<ISaturnDataSqlItemInfo> getSaturnDataSqlItemInfos() {
		return this.subSqlItemInfos;
	}

	public void setSubSqlItemInfos(List<ISaturnDataSqlItemInfo> subSqlItemInfos) {
		this.subSqlItemInfos = subSqlItemInfos;
	}

	public void addSaturnDataSqlItemInfos(ISaturnDataSqlItemInfo sqlItemInfo) {
		this.subSqlItemInfos.add(sqlItemInfo);
	}

	public ISaturnElement toSaturnElement() {
		ISaturnElement saturnElement = new SaturnElement(getType());
		saturnElement.addAttribute("prepend", this.prepend);

		if (this.subSqlItemInfos != null && this.subSqlItemInfos.size() > 0) {

			for (ISaturnDataSqlItemInfo sqlItemInfo : this.subSqlItemInfos) {
				saturnElement.addSubElement(sqlItemInfo.toSaturnElement());
			}
		}
		return saturnElement;
	}

	public String toXmlStr() {
		return toSaturnElement().getXMLStr();
	}

	public abstract String getType();
}
